package com.example.screenrecorder;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class AdvancedSettingsActivity extends AppCompatActivity {
    
    private static final String PREFS_NAME = "advanced_screen_recorder_settings";
    
    private Spinner spinnerResolution;
    private Spinner spinnerFrameRate;
    private Spinner spinnerEngine;
    private Spinner spinnerQuality;
    private CheckBox checkAudio;
    private CheckBox checkCamera;
    private CheckBox checkAnnotation;
    private CheckBox checkPauseResume;
    private CheckBox checkFrontCamera;
    private CheckBox checkBackCamera;
    private CheckBox checkBeautyFilter;
    private SeekBar seekCameraPositionX;
    private SeekBar seekCameraPositionY;
    private SeekBar seekCameraSize;
    private SeekBar seekAnnotationStroke;
    private TextView tvCameraPositionX;
    private TextView tvCameraPositionY;
    private TextView tvCameraSize;
    private TextView tvAnnotationStroke;
    private Button btnTestRecording;
    private Button btnSaveSettings;
    
    private SharedPreferences preferences;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_advanced_settings);
        
        preferences = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        
        initViews();
        setupSpinners();
        setupClickListeners();
        loadSettings();
    }
    
    private void initViews() {
        // 基本设置
        spinnerResolution = findViewById(R.id.spinner_resolution);
        spinnerFrameRate = findViewById(R.id.spinner_framerate);
        spinnerEngine = findViewById(R.id.spinner_engine);
        spinnerQuality = findViewById(R.id.spinner_quality);
        
        // 功能开关
        checkAudio = findViewById(R.id.check_audio);
        checkCamera = findViewById(R.id.check_camera);
        checkAnnotation = findViewById(R.id.check_annotation);
        checkPauseResume = findViewById(R.id.check_pause_resume);
        
        // 摄像头设置
        checkFrontCamera = findViewById(R.id.check_front_camera);
        checkBackCamera = findViewById(R.id.check_back_camera);
        checkBeautyFilter = findViewById(R.id.check_beauty_filter);
        
        // 滑块设置
        seekCameraPositionX = findViewById(R.id.seek_camera_position_x);
        seekCameraPositionY = findViewById(R.id.seek_camera_position_y);
        seekCameraSize = findViewById(R.id.seek_camera_size);
        seekAnnotationStroke = findViewById(R.id.seek_annotation_stroke);
        
        // 显示值
        tvCameraPositionX = findViewById(R.id.tv_camera_position_x);
        tvCameraPositionY = findViewById(R.id.tv_camera_position_y);
        tvCameraSize = findViewById(R.id.tv_camera_size);
        tvAnnotationStroke = findViewById(R.id.tv_annotation_stroke);
        
        // 按钮
        btnTestRecording = findViewById(R.id.btn_test_recording);
        btnSaveSettings = findViewById(R.id.btn_save_settings);
    }
    
    private void setupSpinners() {
        // 分辨率选择器
        ArrayAdapter<RecordingConfig.Resolution> resolutionAdapter = new ArrayAdapter<>(
                this, android.R.layout.simple_spinner_item, RecordingConfig.Resolution.values());
        resolutionAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerResolution.setAdapter(resolutionAdapter);
        
        // 帧率选择器
        ArrayAdapter<RecordingConfig.FrameRate> frameRateAdapter = new ArrayAdapter<>(
                this, android.R.layout.simple_spinner_item, RecordingConfig.FrameRate.values());
        frameRateAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerFrameRate.setAdapter(frameRateAdapter);
        
        // 引擎选择器
        ArrayAdapter<RecordingConfig.RecordingEngine> engineAdapter = new ArrayAdapter<>(
                this, android.R.layout.simple_spinner_item, RecordingConfig.RecordingEngine.values());
        engineAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerEngine.setAdapter(engineAdapter);
        
        // 质量选择器
        ArrayAdapter<RecordingConfig.VideoQuality> qualityAdapter = new ArrayAdapter<>(
                this, android.R.layout.simple_spinner_item, RecordingConfig.VideoQuality.values());
        qualityAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerQuality.setAdapter(qualityAdapter);
    }
    
    private void setupClickListeners() {
        // 摄像头开关监听
        checkCamera.setOnCheckedChangeListener((buttonView, isChecked) -> {
            updateCameraSettingsVisibility(isChecked);
        });
        
        // 标注开关监听
        checkAnnotation.setOnCheckedChangeListener((buttonView, isChecked) -> {
            updateAnnotationSettingsVisibility(isChecked);
        });
        
        // 滑块监听
        seekCameraPositionX.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                tvCameraPositionX.setText("X位置: " + progress + "%");
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {}
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {}
        });
        
        seekCameraPositionY.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                tvCameraPositionY.setText("Y位置: " + progress + "%");
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {}
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {}
        });
        
        seekCameraSize.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                tvCameraSize.setText("大小: " + progress + "%");
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {}
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {}
        });
        
        seekAnnotationStroke.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                tvAnnotationStroke.setText("笔触粗细: " + progress + "px");
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {}
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {}
        });
        
        // 按钮监听
        btnTestRecording.setOnClickListener(v -> testRecording());
        btnSaveSettings.setOnClickListener(v -> saveSettings());
    }
    
    private void updateCameraSettingsVisibility(boolean enabled) {
        int visibility = enabled ? View.VISIBLE : View.GONE;
        checkFrontCamera.setVisibility(visibility);
        checkBackCamera.setVisibility(visibility);
        checkBeautyFilter.setVisibility(visibility);
        seekCameraPositionX.setVisibility(visibility);
        seekCameraPositionY.setVisibility(visibility);
        seekCameraSize.setVisibility(visibility);
        tvCameraPositionX.setVisibility(visibility);
        tvCameraPositionY.setVisibility(visibility);
        tvCameraSize.setVisibility(visibility);
    }
    
    private void updateAnnotationSettingsVisibility(boolean enabled) {
        int visibility = enabled ? View.VISIBLE : View.GONE;
        seekAnnotationStroke.setVisibility(visibility);
        tvAnnotationStroke.setVisibility(visibility);
    }
    
    private void testRecording() {
        // 启动测试录制
        Intent intent = new Intent(this, MainActivity.class);
        intent.putExtra("test_recording", true);
        startActivity(intent);
    }
    
    private void saveSettings() {
        SharedPreferences.Editor editor = preferences.edit();
        
        // 保存基本设置
        editor.putInt("resolution", spinnerResolution.getSelectedItemPosition());
        editor.putInt("framerate", spinnerFrameRate.getSelectedItemPosition());
        editor.putInt("engine", spinnerEngine.getSelectedItemPosition());
        editor.putInt("quality", spinnerQuality.getSelectedItemPosition());
        
        // 保存功能开关
        editor.putBoolean("audio_enabled", checkAudio.isChecked());
        editor.putBoolean("camera_enabled", checkCamera.isChecked());
        editor.putBoolean("annotation_enabled", checkAnnotation.isChecked());
        editor.putBoolean("pause_resume_enabled", checkPauseResume.isChecked());
        
        // 保存摄像头设置
        editor.putBoolean("front_camera_enabled", checkFrontCamera.isChecked());
        editor.putBoolean("back_camera_enabled", checkBackCamera.isChecked());
        editor.putBoolean("beauty_filter_enabled", checkBeautyFilter.isChecked());
        editor.putInt("camera_position_x", seekCameraPositionX.getProgress());
        editor.putInt("camera_position_y", seekCameraPositionY.getProgress());
        editor.putInt("camera_size", seekCameraSize.getProgress());
        
        // 保存标注设置
        editor.putInt("annotation_stroke_width", seekAnnotationStroke.getProgress());
        
        editor.apply();
        
        // 显示保存成功消息
        android.widget.Toast.makeText(this, "设置已保存", android.widget.Toast.LENGTH_SHORT).show();
    }
    
    private void loadSettings() {
        // 加载基本设置
        spinnerResolution.setSelection(preferences.getInt("resolution", 1)); // 默认1080p
        spinnerFrameRate.setSelection(preferences.getInt("framerate", 2)); // 默认30fps
        spinnerEngine.setSelection(preferences.getInt("engine", 0)); // 默认标准引擎
        spinnerQuality.setSelection(preferences.getInt("quality", 2)); // 默认高质量
        
        // 加载功能开关
        checkAudio.setChecked(preferences.getBoolean("audio_enabled", true));
        checkCamera.setChecked(preferences.getBoolean("camera_enabled", false));
        checkAnnotation.setChecked(preferences.getBoolean("annotation_enabled", false));
        checkPauseResume.setChecked(preferences.getBoolean("pause_resume_enabled", false));
        
        // 加载摄像头设置
        checkFrontCamera.setChecked(preferences.getBoolean("front_camera_enabled", true));
        checkBackCamera.setChecked(preferences.getBoolean("back_camera_enabled", false));
        checkBeautyFilter.setChecked(preferences.getBoolean("beauty_filter_enabled", false));
        seekCameraPositionX.setProgress(preferences.getInt("camera_position_x", 50));
        seekCameraPositionY.setProgress(preferences.getInt("camera_position_y", 50));
        seekCameraSize.setProgress(preferences.getInt("camera_size", 30));
        
        // 加载标注设置
        seekAnnotationStroke.setProgress(preferences.getInt("annotation_stroke_width", 5));
        
        // 更新显示
        updateCameraSettingsVisibility(checkCamera.isChecked());
        updateAnnotationSettingsVisibility(checkAnnotation.isChecked());
        
        // 更新滑块显示
        tvCameraPositionX.setText("X位置: " + seekCameraPositionX.getProgress() + "%");
        tvCameraPositionY.setText("Y位置: " + seekCameraPositionY.getProgress() + "%");
        tvCameraSize.setText("大小: " + seekCameraSize.getProgress() + "%");
        tvAnnotationStroke.setText("笔触粗细: " + seekAnnotationStroke.getProgress() + "px");
    }
}
